/* $Id: main.c 10 2011-11-21 11:42:38Z garretb26 $
 * $Date: 2011-11-21 11:42:38 +0000 (Mon, 21 Nov 2011) $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "japi.h"

#define NMAX 100

int string_to_int( int *arr, char *bigstring ) {
    /* Divide the string into numbers */
    int n ;
    char *s = strtok( bigstring, "\r\n");
    for (n=0; s!=NULL && n<NMAX ; n++) {
        arr[n] = atoi(s) ;
        s = strtok(NULL, "\r\n") ;
    }
    return n ;
}

/*
 * Event handler to run the min() function
 * Args:
 *  the textarea with the numbers in
 *  The answer object to put the answer into
 * returns:
 *  1 on success
 */
int min_EH( int textarea, int answer_label) {
    int n, answer ;
    char number_string[NMAX*10], label_text[30] ;
    int numbers[NMAX] ;

    j_gettext( textarea, number_string ) ;
    n = string_to_int( numbers, number_string ) ;

    answer = numbers[0];

    sprintf(label_text, "Minimum: %d", answer) ;
    j_settext(answer_label, label_text) ;

    j_show(answer_label) ;
    return 1 ;
}

int max_EH( int textarea, int answer_label) {
    int n, answer ;
    char string[NMAX*10], label_text[30] ;
    int numbers[NMAX] ;
    j_gettext( textarea, string ) ;
    n = string_to_int( numbers, string ) ;
    answer = numbers[1] ;
    sprintf(label_text, "Maximum: %d", answer) ;
    j_settext(answer_label, label_text) ;
    j_show(answer_label) ;
    return 1 ;
}

int average_EH( int textarea, int answer_label) {
    int n, answer ;
    char string[NMAX*10], label_text[30] ;
    int numbers[NMAX] ;
    j_gettext( textarea, string ) ;
    n = string_to_int( numbers, string ) ;
    answer = numbers[2];
    sprintf(label_text, "Average: %d", answer) ;
    j_settext(answer_label, label_text) ;
    j_show(answer_label) ;
    return 1 ;
}

int median_EH( int textarea, int answer_label) {
    int n, answer ;
    char string[NMAX*10], label_text[30] ;
    int numbers[NMAX] ;
    j_gettext( textarea, string ) ;
    n = string_to_int( numbers, string ) ;
    answer = numbers[3] ;
    sprintf(label_text, "Median: %d", answer) ;
    j_settext(answer_label, label_text) ;
    j_show(answer_label) ;
    return 1 ;
}



int main()
{
    /* graphical Objects */
    int frame,panel,obj;
    int textarea;
    int button_panel, min_button, max_button, ave_button, med_button, answer_label ;

    j_setdebug(3);

    if(!j_start())
        printf("can't connect to server\n"),exit(0);

    /* Generate Graphical Objects */
    frame   = j_frame("Statistics");

    j_setgridlayout(frame,1,2);

    /* left-hand panel */
    panel   = j_panel(frame);
    j_setgridlayout(panel,1,1);
    textarea    = j_textarea(panel,15,4);
    j_setrows(textarea,13);
    j_setcolumns(textarea,10);

    /* right-hand panel */
    button_panel = j_panel(frame) ;
    j_setgridlayout(button_panel,5,3);
    min_button  = j_button(button_panel, "min") ;
    max_button  = j_button(button_panel, "max") ;
    ave_button  = j_button(button_panel, "average") ;
    med_button  = j_button(button_panel, "median") ;
    answer_label = j_label(button_panel, "-----------------") ;

    j_pack(frame);
    j_show(frame);

	while(1)
    {
        fflush(stdout);
        obj=j_nextaction();

        if (obj == frame)
        {
            j_quit();
            break;
        }
        else if(obj==min_button) {
            min_EH( textarea, answer_label ) ;
        }
        else if(obj==max_button) {
            max_EH( textarea, answer_label ) ;
        }
        else if(obj==ave_button) {
            average_EH( textarea, answer_label ) ;
        }
        else if(obj==med_button) {
            median_EH( textarea, answer_label ) ;
        }

    }

    exit(0);
}

int foo_garret() {
    return 1 ;
}
